﻿<%@ Page Language="C#" AutoEventWireup="true"%>
<%
    // get message from fibosms gateway
    string message = Request.Params["message"]; // get message content
    string phone = Request.Params["phone"]; // phone number of customer
    string service = Request.Params["service"]; // service code
    string port = Request.Params["port"];  // port number

    // string main = Request.Params["main"];  //keyword
    string sub = Request.Params["sub"];  // prefix
    // main solution
    string currentport = "8022"; // our current registry port
    string currentkeyword = "SMS";
    string currentprefix = "HALO";
    string currentcontent = "HOBA";
    string uniquecode = GetUniqueString();// unique id for each sms by using md5 encode current datetime
    string responemsg = ""; // what we want to response to cutomer
    if (port != currentport)  //wrong port number, return message to sms gateway
    {
        responemsg = "Ban da nhan sai dau so";
    }
    else
    {
        message = message.ToUpper(); // convert to upper case.
        string[] msgarray = message.Split(new char[] { ' ' });//split message content.
        if (msgarray.Length != 3)
            responemsg = "xin chao, ban vui long nhan tin theo cu phap sms halo hoba";
        else
        {
            string keyword = msgarray[0];  // message keyword.
            string prefix = msgarray[1]; // message prefix
            string msgcontent = msgarray[2];// message content.
            if (currentkeyword == keyword && currentprefix == prefix && currentcontent == msgcontent)
            {
                responemsg = "cam on ban da su dung dich vu cua HOBA, chuc ban thanh cong";

                // luu tin nhan
                //string xmlContent = "<SMS>";
                string xmlContent = "<MESSAGE>" + message + "</MESSAGE>";
                xmlContent += "<PHONE>" + phone + "</PHONE>";
                xmlContent += "<SERVICE>" + service + "</SERVICE>";
                xmlContent += "<PORT>" + port + "</PORT>";
                xmlContent += "<DATE>" + DateTime.Now.ToString() + "</DATE>";
                //xmlContent += "</SMS>";

                System.Xml.XmlDocument m = new System.Xml.XmlDocument();
                m.Load(Server.MapPath("~/messages.xml"));

                System.Xml.XmlElement root = m.DocumentElement;
                System.Xml.XmlElement sms = m.CreateElement("SMS");
                sms.InnerXml = xmlContent;
                root.AppendChild(sms);

                m.Save(Server.MapPath("~/messages.xml"));
            }
            else
                responemsg = "xin chao, ban vui long nhan tin theo cu phap sms halo hoba";
        }
        //response to sms gateway
    }
    Response.Write(
                   "<ClientResponse>" +
                       "<Message>" +
                           "<PhoneNumber>" + phone + "</PhoneNumber>" +
                           "<Message>" + responemsg + "</Message>" +
                           "<SMSID>" + uniquecode + "</SMSID>" +
                           "<ServiceNo>" + service + "</ServiceNo>" +
                           "</Message>" +
                   "</ClientResponse>"
                  );
     %>
<script runat="server">
        protected string GetUniqueString()
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();// md5 object
            System.Text.UnicodeEncoding u = new System.Text.UnicodeEncoding();
            byte[] uniquekey = md5.ComputeHash(u.GetBytes(System.DateTime.Now.ToString()));
            return Convert.ToBase64String(uniquekey);
        }
</script>
